/*
 * @FilePath: /web-vue/src/store/index.js
 * @Description: 缓存管理
 */
import Vue from "vue";
import VueCookies from "vue-cookies";
import Vuex from "vuex";

Vue.use(Vuex);
Vue.use(VueCookies);

// 从 localStorage 获取用户会话，且从Cookie 获取 Token
const getUserSession = () => {
  if ($cookies.get("AUTH_TOKEN") && localStorage["userSession"]) {
    return JSON.parse(localStorage.getItem("userSession"));
  }

  return null;
};

// 根据角色定义默认路由
const defaultRouteUrl = () => {
  // 获取用户会话
  let userSession = getUserSession();
  if (userSession) {
    return "/";
  }

  return "/"; // 默认布局页
};

// 全局属性
const getDefaultState = () => {
  return {
    stateTagsList: [],
    // 用户会话信息
    userSession: getUserSession(),
    // 最近一次路由
    latestRouteUrl: defaultRouteUrl(),
    // 字典信息
    DICT: {
      // 系统用户列表
      sysUserList: [],
      // 部门列表
      deptList: [],
      // 角色列表
      sysRoleList: [],
      // 数据域
      dataScopeSelect: [
        { name: "全部数据", code: "DS_01", theme: "" },
        { name: "本组织及下级数据", code: "DS_02", theme: "success" },
        { name: "本组织数据", code: "DS_03", theme: "danger" },
        { name: "仅本人数据", code: "DS_05", theme: "info" },
      ],
      // 性别
      sex: { 1: "男", 0: "女" },
      // api状态码
      apiStatusSelect: [
        { name: "正常", code: 200 },
        { name: "资源无变化", code: 304 },
        { name: "请求有误", code: 400 },
        { name: "身份未认证", code: 401 },
        { name: "禁止访问", code: 403 },
        { name: "资源不存在", code: 404 },
        { name: "内部错误", code: 500 },
      ],
      // ----------------- 业务模块字典码值表 --------------------
      // ------- 房源 -------
      // 房源类型
      houseTypeSelect: [],
      // 地区码
      regionCode: [],
      // 房源列表Map
      houseMap: {},
      // 房源状态
      houseStatusSelect: [
        { name: "待审批", code: 1, theme: "info" },
        { name: "审批通过", code: 2, theme: "" },
        { name: "审批不通过", code: 3, theme: "danger" },
        { name: "空闲中", code: 4, theme: "success" },
        { name: "已预定", code: 5, theme: "danger" },
        { name: "已入住", code: 6, theme: "warning" },
      ],
      // ------- 租房订单 -------
      // 是否需要餐饮
      isNeedMealSelect: [
        { name: "是", code: "1", theme: "" },
        { name: "否", code: "0", theme: "info" },
      ],
      // 订单状态
      orderStatusSelect: [
        { name: "待支付", code: 1, theme: "info" },
        { name: "已支付", code: 2, theme: "" },
        // { name: "待入住", code: 3, theme: "warning" },
        // { name: "入住中", code: 4, theme: "" },
        { name: "待评价", code: 5, theme: "success" },
        { name: "已评价", code: 6, theme: "" },
      ],
      // ------- 房源图片，首页轮播图 -------
      // 图片状态
      imageStatusSelect: [
        { name: "显示", code: 1, theme: "" },
        { name: "隐藏", code: 2, theme: "info" },
      ],
      // ------- 餐饮 -------
      // 餐饮状态
      mealStatusSelect: [
        { name: "正常供应", code: 1, theme: "" },
        { name: "已售罄", code: 4, theme: "danger" },
        { name: "已失效", code: 5, theme: "" },
      ],
      // ------- 地区 -------
      // 是否中高风险地区
      isHighRiskSelect: [
        { name: "是", code: "1", theme: "danger" },
        { name: "否", code: "0", theme: "success" },
      ],
      // ------- 旅游景点 -------
      // 景点类型
      sceneryTypeSelect: [
        { name: "山岳风景区", code: "1", theme: "success" },
        { name: "森林风景区", code: "2", theme: "success" },
        { name: "山水风景区", code: "3", theme: "success" },
        { name: "湖泊风景区", code: "4", theme: "" },
        { name: "海滨风景区", code: "5", theme: "" },
        { name: "文化遗产胜地", code: "6", theme: "" },
        { name: "休闲疗养避暑胜地", code: "7", theme: "" },
        { name: "宗教寺庙名胜区", code: "8", theme: "" },
        { name: "革命纪念地", code: "9", theme: "" },
        { name: "其他", code: "10", theme: "" },
      ],
      // 景点状态
      sceneryStatusSelect: [
        { name: "正常开放", code: 1, theme: "success" },
        { name: "临时管控", code: 2, theme: "warning" },
        { name: "暂未开放", code: 3, theme: "danger" },
      ],
      // ------- 系统参数 -------
      // 参数状态
      configStatusSelect: [
        { name: "启用", code: 1, theme: "" },
        { name: "禁用", code: 2, theme: "info" },
      ],
    },
    // 接口根路径
    //API_ROOT: "http://106.15.125.227:80/",
    //API_ROOT: "http://localhost:8088/",
    API_ROOT: "https://file-stora.oss-cn-chengdu.aliyuncs.com/",
    API_ROOTT: "http://106.15.125.227:8088/",
    // 每日通知提醒
    notice: {
      needTodo: 0,
    },
    // 状态与权限
    auth: {
      isDelete: false,
      isAdd: false,
      isEdit: false,
      isAppraise: false,
    },
  };
};

// initial state
const state = getDefaultState();

export default new Vuex.Store({
  state,
  mutations: {
    /**
     * 选择tag标签
     */
    mutationSelectTags(state, data) {
      let result = false;
      for (let i = 0; i < state.stateTagsList.length; i++) {
        if (state.stateTagsList[i].href == data.href) {
          return (result = true);
        }
      }
      result === false ? state.stateTagsList.push(data) : "";
    },

    /**
     * 关闭tag标签
     */
    mutationCloseTag(state, url) {
      let result = state.stateTagsList.findIndex((item) => item.href === url);
      state.stateTagsList.splice(result, 1);
    },

    // 初始化全局属性
    initState() {
      // 初始化状态属性
      Object.assign(state, getDefaultState());
    },

    // 重置全局属性
    resetState() {
      // 清除用户会话
      localStorage.removeItem("userSession");
      // 初始化状态属性
      Object.assign(state, getDefaultState());
    },
  },

  actions: {},
  modules: {},
});
